package com.sk.seckilldemo.exception;

import com.sk.seckilldemo.enums.ResultCodeEnum;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * 自定义业务异常处理类（专为秒杀系统优化）
 * */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class CustomException extends RuntimeException {
    private Integer code; //状态码
    private String msg; //系统消息

    /**
     * 通过枚举构造异常错误信息
     * */
    public CustomException(ResultCodeEnum resultCodeEnum){
        super(resultCodeEnum.getMsg());
        this.code=resultCodeEnum.getCode();
        this.msg=resultCodeEnum.getMsg();
    }

    /**
     * 支持自定义消息
     * */
    public CustomException(ResultCodeEnum resultCodeEnum,String customMsg){
        super(customMsg);
        this.code = resultCodeEnum.getCode();
        this.msg = resultCodeEnum.getMsg();
    }

    /**
     * 快速构建库存不足的异常
     * */
    public static CustomException stockNotEnough(){
        return new CustomException(ResultCodeEnum.STOCK_NOT_ENOUGH);
    }
}
